package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.support.ui.ExpectedConditions;

import java.io.IOException;
import java.sql.Driver;

public class DatailPage extends Utils {
    public static String url = detailUrl;
    public DatailPage() {
        super(url);
    }

    //校验详情页是否正常加载
    public void checkDetailPage() {
        //校验标题是否正确
        String title  =driver.findElement(By.cssSelector("body > div.container > div.right > div > div.title")).getText();
        //发布时间
        String time = driver.findElement(By.cssSelector("body > div.container > div.right > div > div.date")).getText();

        //编辑按钮是否存在
       String edit =  driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(1)")).getText();
        //删除按钮是否存在
        String delete =  driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(2)")).getText();
        //校验标题、时间、内容、编辑按钮、删除按钮非空
        assert !title.isEmpty() ;
        assert !time.isEmpty();

        assert edit.equals("编辑");
        assert delete.equals("删除");
        System.out.println("详情页校验通过");
    }
    //编辑按钮是否正常跳转到编辑页面
    public void editButton() throws InterruptedException {
        //编辑前的标题
        String edit =  driver.findElement(By.cssSelector("body > div.container > div.right > div > div.title")).getText();
        //点击编辑按钮
        driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(1)")).click();
        Thread.sleep(3000);
        //校验是否跳转到编辑页面
        String editjump = driver.findElement(By.cssSelector("#title")).getText();
        //校验
        assert editjump.equals(edit);

        //返回到详情页面
        driver.navigate().back();
        System.out.println("编辑按钮正常跳转");
    }
    //删除按钮是否正常删除
    public void deleteButton() {
        //点击删除按钮
        driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(2)")).click();
        //出现提示框

        Alert alert = driver.switchTo().alert();
        //获取弹窗内容
        String alertText = alert.getText();
        System.out.println("弹窗内容：" + alertText);
        //点击取消按钮
        // alert.dismiss();
        //点击确定按钮
        alert.accept();
        //校验是否删除成功
        try {
            wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(2)")));
            System.out.println("删除成功");
        } catch (TimeoutException e) {
            System.out.println("删除失败");
        }
    }
}
